﻿Public Class FrmBCentro
    Private Shared costo As GBDZafiro.CentroCosto

    Public ReadOnly Property ctrCosto As GBDZafiro.CentroCosto
        Get
            Return costo
        End Get
    End Property

    Private Sub FrmBCentro_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargarGvCtrCosto(GBDZafiro.CentroCosto.ConsultarAll())
        tbFiltro.Focus()
    End Sub

    Private Sub btAceptar_Click(sender As System.Object, e As System.EventArgs) Handles btAceptar.Click
        cargarGvCtrCosto(GBDZafiro.CentroCosto.ConsxDescripcion(tbFiltro.Text))
    End Sub

    Private Sub btCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btCancelar.Click
        costo = Nothing
        Me.Close()
    End Sub

    Private Sub tbFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbFiltro.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            cargarGvCtrCosto(GBDZafiro.CentroCosto.ConsxDescripcion(tbFiltro.Text))
        End If
    End Sub

    Private Sub gvCosto_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gvCosto.CellDoubleClick
        sendCtrCosto()
    End Sub

    Private Sub gvCosto_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles gvCosto.KeyDown
        If e.KeyCode = Keys.Enter Then
            sendCtrCosto()
        End If
    End Sub

    Private Sub gvCosto_CurrentCellChanged(sender As System.Object, e As System.EventArgs) Handles gvCosto.CurrentCellChanged
        Try

        Catch ex As Exception

        End Try
    End Sub

    Private Sub sendCtrCosto()
        Dim ctrCosto As New GBDZafiro.CentroCosto
        Dim rGvCtrCosto As New DataGridViewRow()
        rGvCtrCosto = gvCosto.CurrentRow

        ctrCosto.COD_CENTRO = rGvCtrCosto.Cells.Item("COD_CENTRO").Value
        ctrCosto.DESCRIPCION = rGvCtrCosto.Cells.Item("DESCRIPCION").Value

        costo = ctrCosto
        DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub cargarGvCtrCosto(ByRef dtCtrCosto As DataTable)
        If dtCtrCosto.Rows.Count < 1 Then
            lbNoDatos.Visible = True
            gvCosto.DataSource = Nothing

        Else
            lbNoDatos.Visible = False
            gvCosto.DataSource = Nothing
            gvCosto.Rows.Clear()
            gvCosto.DataSource = dtCtrCosto
        End If
    End Sub

End Class